package au.com.handsunited.web.validator.project;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import au.com.handsunited.model.validators.EmailAddressValidator;
import au.com.handsunited.web.command.project.InviteProjectCommand;

/**
 * @author arunda
 *
 */
public class InviteProjectCommandValidator implements Validator {


	private static Logger logger = LoggerFactory.getLogger(InviteProjectCommandValidator.class);



	/**
	 *
	 */
	public InviteProjectCommandValidator() {
		super();
	}

	/**
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	@Override
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(InviteProjectCommand.class);
	}

	/**
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	public void validate(Object target, Errors errors) {
		InviteProjectCommand command = (InviteProjectCommand) target;

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "projectName", "inviteProjectCommand.projectName.required", "Project Name is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "contactPerson", "inviteProjectCommand.contactPerson.required", "Contact Person is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "inviteProjectCommand.emailAddress.required", "Email Address is required.");

		if (StringUtils.isNotBlank(command.getEmailAddress()) && !EmailAddressValidator.isValid(command.getEmailAddress())) {
			 errors.rejectValue("emailAddress", "inviteProjectCommand.emailAddress.invalid", "Email Address is invalid");
		}
	}

}
